package main.Primavera;

import com.primavera.ServerException;
import com.primavera.integration.client.Session;
import com.primavera.integration.client.bo.BOIterator;
import com.primavera.integration.client.bo.BusinessObjectException;
import com.primavera.integration.client.bo.object.EPS;
import com.primavera.integration.client.bo.object.Project;
import com.primavera.integration.network.NetworkException;
import main.Trees.ProjectNode;

import java.util.ArrayList;

public class PrimaveraOpenProject {

    private Session session;
    private ProjectNode root;

    private ArrayList<EPS> epsTable;
    private ArrayList<Project> projectTable;

    private void fillEpsTable () {

        this.epsTable = new ArrayList<> ();

        try {

            BOIterator<EPS> boiEPS = session.getGlobalObjectManager().loadEPS(new String[]
                    {
                            "ObjectId", "Name", "Id", "ParentObjectId"
                    }, null, null);

            while (boiEPS.hasNext()) {
                this.epsTable.add(boiEPS.next());
            }

        } catch (ServerException | NetworkException | BusinessObjectException e) {
            e.printStackTrace();
        }

    }

    private void fillProjectTable () {

        this.projectTable = new ArrayList<> ();

        try {

            BOIterator<Project> boiProjects = session.getGlobalObjectManager().loadProjects(new String[]
                    {
                            "ObjectId", "Name", "Id", "ParentEPSObjectId"
                    }, null, null);

            while (boiProjects.hasNext()) {
                this.projectTable.add(boiProjects.next());
            }

        } catch (ServerException | NetworkException | BusinessObjectException e) {
            e.printStackTrace();
        }

    }

    public PrimaveraOpenProject ( Session session ) {

        this.session = session;

        fillEpsTable();
        fillProjectTable();

        this.root = new ProjectNode("Primavera P6 Structure");

        displayHierarchy();

    }

    public ProjectNode getRoot () { return root; }

    private void displayHierarchy() {
        try {

            for (EPS eps : epsTable) {
                if (eps.getParentObjectId() == null) {
                    ProjectNode obj = new ProjectNode( eps.getName()+" - "+eps.getId(), eps.getId(), eps.getObjectId().toInteger(), false);
                    obj.setAllowChildren(true);
                    root.add(obj);
                    addChildren(obj);
                }
            }

        } catch (BusinessObjectException e) {
            e.printStackTrace();
        }

    }

    private void addChildren(ProjectNode node) {
        try {

            for (EPS eps : epsTable) {
                if (eps.getParentObjectId()!= null && eps.getParentObjectId().toInteger() == node.getId()) {
                    ProjectNode obj = new ProjectNode(eps.getId()+" - "+ eps.getName(), eps.getId(), eps.getObjectId().toInteger(), false);
                    obj.setAllowChildren(true);
                    node.add(obj);
                    addChildren(obj);
                }
            }

            for (Project project : projectTable) {
                if (project.getParentEPSObjectId().toInteger() == node.getId()) {
                    ProjectNode obj = new ProjectNode(project.getId()+" - "+project.getName(), project.getId(), project.getObjectId().toInteger(), true );
                    obj.setAllowChildren(false);
                    node.add (obj);
                }
            }

        } catch (BusinessObjectException e) {
            e.printStackTrace();
        }

    }

}
